﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.BasicView
{
    public partial class Mall : BasePage
    {
        private MallPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new MallPresenter();
            if (!IsPostBack)
            {
                PrepareView();
            }
        }


        protected void UpdateProfileButton_Click(object sender, EventArgs e)
        {
            DTOMall Mall = PopulateObject();
            if (_presenter.SetDataMall(Mall))
            {
                ShowMessage(LanguageLoader.GetText("MALL_SAVE_SUCCESS"), LanguageLoader.GetText("SUCCESS"));
            }
            else
            {
                ShowMessage(LanguageLoader.GetText("FAILED_OPERATION_MESSAGE"), LanguageLoader.GetText("FAILED"));
            }
        }

        private void PrepareView()
        {
            DTOMall mall = _presenter.GetMalls().FirstOrDefault();
            if (mall != null)
                PopulateControls(mall);
        }

        public int Id
        {
            set { ViewState["Id"] = value; }
            get { return ViewState["Id"] != null ? Convert.ToInt32(ViewState["Id"]) : 0; }
        }

        private void PopulateControls(DTOMall mall)
        {
            Name.Text = mall.Name;
            if(mall.Logo != null)
            {
                imgCell.Visible = true;
            }
            
            //LoginId.Text = mall.LoginId;
            Address.Text = mall.Address;
            Telephone.Text = mall.Telephone;
            Email.Text = mall.Email;
            ContactPerson.Text = mall.ContactPerson;
            Description.Text = mall.Description;
            Id = mall.Id;
        }
        private DTOMall PopulateObject()
        {
            DTOMall mall = new DTOMall();
            mall.Name = Name.Text;
            if (fuLogo.PostedFile != null && fuLogo.PostedFile.ContentLength > 0)
            {
                mall.FileName = GetFileName(fuLogo.PostedFile.FileName);
                mall.FileType = fuLogo.PostedFile.ContentType;
                int len = fuLogo.PostedFile.ContentLength;
                byte[] file = new byte[len];
                fuLogo.PostedFile.InputStream.Read(file, 0, len);
                mall.Logo = file;
            }
            else
            {
                mall.Logo = null;
            }
            mall.Address = Address.Text;
            mall.Telephone = Telephone.Text;
            mall.Email = Email.Text;
            mall.ContactPerson = ContactPerson.Text;
            mall.Description = Description.Text;
            mall.Id = Id;
            mall.IsNew = mall.Id <= 0;
            mall.LastUpdated = DateTime.Now;
            return mall;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            SetLanguageAndResource();
        }

        private void SetLanguageAndResource()
        {
            NameLable.Text = LanguageLoader.GetText("NAME");
            LogoLable.Text = LanguageLoader.GetText("LOGO");
            AddressLable.Text = LanguageLoader.GetText("ADDRESS");
            TelephoneLable.Text = LanguageLoader.GetText("TELEPHONE");
            EmailLabel.Text = LanguageLoader.GetText("EMAIL");
            ContactPersonLabel.Text = LanguageLoader.GetText("CONTACT_PERSON");
            DescriptionLable.Text = LanguageLoader.GetText("DESCRIPTION");
            SaveMall.Text = LanguageLoader.GetText("SAVE");
            LogoDisplayLabel.Text = LanguageLoader.GetText("LOGO");
        }
    }
}